package com.zhengdongzulin.zuke.app.main.activies


import android.content.Intent
import android.text.TextUtils
import androidx.lifecycle.Observer
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.androidktx.core.*
import com.xuexiang.xui.widget.banner.widget.banner.BannerItem
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.City
import com.zhengdongzulin.zuke.app.data.activities.ActivitiesBean
import com.zhengdongzulin.zuke.app.main.ZKWebviewActivity
import com.zhengdongzulin.zuke.base.BaseFragment
import com.zhengdongzulin.zuke.databinding.ZkFragmentMainActiveBinding
import com.zhengdongzulin.zuke.ext.loadImage
import com.zhengdongzulin.zuke.ext.put
import com.zhengdongzulin.zuke.ext.start
import java.util.regex.Matcher
import java.util.regex.Pattern

/**
 * jeff
 * 活动
 * 2021-05-18 11:01:51
 */
class ZKActiviesFragment : BaseFragment<ActiviesVm, ZkFragmentMainActiveBinding>() {

    companion object {
        fun newInstance() = ZKActiviesFragment()
    }

    override fun getLayoutResId(): Int = R.layout.zk_fragment_main_active

    override fun initVM(): ActiviesVm = getVM(ActiviesVm::class.java)

    override fun initView() {
        mView.statusView.showContent()
        hListAdpater = ActivtiesListAdpater(arrayListOf())
        hListAdpater.setOnItemClickListener { adapter, view, position ->
            Intent().put("type", "活动")
                .putExtra("activeBean", hListAdpater.data[position])
                .start(this.requireActivity(), ZKWebviewActivity::class.java)
        }
        mView.rvActives.vertical().adapter = hListAdpater
    }

    override fun initData() {
        var current_city = sp().getString("current_city", "")
        var mCity = current_city?.toBean<City>()
        mViewModel.get_banner_pic_list(mCity?.cityId)
        mViewModel.get_activity_list(mCity?.cityId, startPage)
    }

    private lateinit var hListAdpater: ActivtiesListAdpater
    var startPage = 1
    override fun initListener() {
        val cityId = mShareViewModel?.city?.value?.cityId
        mView.refreshLayout.setOnRefreshListener {
            startPage = 1
            mViewModel.get_banner_pic_list(cityId)
            mViewModel.get_activity_list(cityId, startPage)
        }
        mView.refreshLayout.setOnLoadMoreListener {
            mViewModel.get_activity_list(cityId, startPage)
        }
    }

    override fun startObserve() {
        mViewModel.get_activity_list.observe(this, Observer {

            mView.refreshLayout.finishRefresh()

            if (it != null && it.records != null) {
                if (startPage == 1) {
                    hListAdpater.setList(it.records)
                } else {
                    hListAdpater.addData(it.records)
                }
                mView.refreshLayout.setEnableLoadMore(it.records.size == 10)
            }

        })
        mViewModel.get_activity_list.state.observe(
            this,
            getObserver(mViewModel.get_activity_list)
        )

        mViewModel.get_banner_pic_list.observe(this, Observer {
            mView.refreshLayout.finishRefresh()

            if (it != null && it.records != null) {
                mView.banner.visible()
                var imagesData = mutableListOf<BannerItem>()
                for (banner in it.records) {
                    imagesData.add(BannerItem().setImgUrl(banner.pic))
                }
                mView.banner.setSource(imagesData)?.startScroll()

                mView.banner.setOnItemClickListener { view, item, position ->

                    if (!TextUtils.isEmpty(it.records[position].url)) {
                        Intent().put("title", it.records[position].title)
                            .put("url", it.records[position].url)
                            .start(this.requireActivity(), ZKWebviewActivity::class.java)
                    }

                }
            } else {
                mView.banner.gone()
            }

        })
        mViewModel.get_banner_pic_list.state.observe(
            this,
            getObserver(mViewModel.get_banner_pic_list)
        )
    }

    inner class ActivtiesListAdpater(
        list: MutableList<ActivitiesBean.Bean>
    ) : BaseQuickAdapter<ActivitiesBean.Bean, BaseViewHolder>(
        R.layout.item_main_activities,
        list
    ) {
        override fun convert(holder: BaseViewHolder, item: ActivitiesBean.Bean) {
            holder.loadImage(R.id.iv_1, item.pic, R.mipmap.error_empty_zheng, 5f)
            holder.setText(R.id.name, item.title)
            holder.setText(R.id.tv_des, Html2Text(item.content))
        }
    }


    //从html中提取纯文本
    fun Html2Text(inputString: String): String? {
        var htmlStr = inputString // 含html标签的字符串
        var textStr = ""
        val p_script: Pattern
        val m_script: Matcher
        val p_style: Pattern
        val m_style: Matcher
        val p_html: Pattern
        val m_html: Matcher
        try {
            val regEx_script =
                "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>" // 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
            val regEx_style =
                "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>" // 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
            val regEx_html = "<[^>]+>" // 定义HTML标签的正则表达式
            p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE)
            m_script = p_script.matcher(htmlStr)
            htmlStr = m_script.replaceAll("") // 过滤script标签
            p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE)
            m_style = p_style.matcher(htmlStr)
            htmlStr = m_style.replaceAll("") // 过滤style标签
            p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE)
            m_html = p_html.matcher(htmlStr)
            htmlStr = m_html.replaceAll("") // 过滤html标签
            textStr = htmlStr
        } catch (e: Exception) {
            System.err.println("Html2Text: " + e.message)
        }
        //剔除空格行
        textStr = textStr.replace("[ ]+".toRegex(), " ")
        textStr = textStr.replace("(?m)^\\s*$(\\n|\\r\\n)".toRegex(), "")
        return textStr // 返回文本字符串
    }


}